package br.com.medmark.model.hibernate.dao;

import java.util.List;

import javax.persistence.NoResultException;
import javax.persistence.TypedQuery;

import br.com.medmark.model.hibernate.Consulta;

public class ConsultaDAO extends GenericoDAOImpl<Consulta> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4424491407880716177L;
	
	private static ConsultaDAO consultaDAO = null;
	
	private ConsultaDAO(){
		
	}
	
	public static ConsultaDAO getInstance(){
		
		if ( consultaDAO == null) {
			consultaDAO = new ConsultaDAO();
		}
	
		return consultaDAO;
		
	}
	
	
	public List<Consulta> getConsultas(int codigoClinica) {
		String sql = "SELECT c FROM Consulta c,Solicitacao s,Horario h Where c.solicitacao.codigo = s.codigo and s.horario.codigo = h.codigo and h.clinica.codigo = :codigoClinica";
		
		TypedQuery<Consulta> query = this.entityManager.createQuery(sql, Consulta.class);
		query.setParameter("codigoClinica", codigoClinica);
	
		try {
			return query.getResultList();
		} catch (NoResultException ex) {
			return null;
		}
	}
	
	public List<Consulta> getConsultasConfirmadas(int codigoClinica) {
		String sql = "SELECT c FROM Consulta c,Solicitacao s,Horario h Where c.solicitacao.codigo = s.codigo and s.horario.codigo = h.codigo and h.clinica.codigo = :codigoClinica and c.status = 1";
		
		TypedQuery<Consulta> query = this.entityManager.createQuery(sql, Consulta.class);
		query.setParameter("codigoClinica", codigoClinica);
	
		try {
			return query.getResultList();
		} catch (NoResultException ex) {
			return null;
		}
	}
	
	

}
